package com.cn.customer.housekeeping.housekeepingb.ui;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.cn.customer.housekeeping.housekeepingb.R;
import com.cn.customer.housekeeping.housekeepingb.bean.BaseResp;
import com.cn.customer.housekeeping.housekeepingb.bean.FriendRequestListResp;
import com.cn.customer.housekeeping.housekeepingb.biz.personcenter.IUserLoginView;
import com.cn.customer.housekeeping.housekeepingb.biz.personcenter.InterfacePresenter;
import com.cn.customer.housekeeping.housekeepingb.constant.URLUtil;
import com.cn.customer.housekeeping.housekeepingb.ui.base.BaseActivity;
import com.cn.customer.housekeeping.housekeepingb.ui.base.QuickAdapter;
import com.cn.customer.housekeeping.housekeepingb.ui.helper.ItemHelper;
import com.cn.customer.housekeeping.housekeepingb.util.JsonAnalysisUtil;
import com.cn.customer.housekeeping.housekeepingb.util.ToastUtil;
import com.cn.customer.housekeeping.housekeepingb.util.Utils;
import com.lfq.pulltorefresh.library.PullToRefreshBase;
import com.lfq.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;


/***
 *
 */
public class DemoListActivity extends BaseActivity implements IUserLoginView {


    private InterfacePresenter mInterfacePresenter;
    private ListView mListView;
    private PullToRefreshListView mPullToRefreshListView;
    private int pageNum = 1;
    private int pageSize = 20;
    TextView tvwu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main_list);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initViews() {
        mInterfacePresenter = new InterfacePresenter();
        mInterfacePresenter.attachView(this);
        mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        tvwu=(TextView)findViewById(R.id.tvwu);
        mPullToRefreshListView.setMode(PullToRefreshBase.Mode.DISABLED);
        mListView = mPullToRefreshListView.getRefreshableView();
        mListView.setHeaderDividersEnabled(false);
        mListView.setDividerHeight(0);
    }

    @Override
    public void initListeners() {

        mPullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageNum = 1;

//                mInterfacePresenter.getBlacklist(pageNum, pageSize);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
//                mInterfacePresenter.getBlacklist(pageNum, pageSize);
            }
        });

        mPullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int headerCount = mListView.getHeaderViewsCount();
                if (position >= headerCount) {
//                    FriendRequestListResp item = mAdapter.getItem(position - headerCount);
//                    UserInfo   userInfo = new UserInfo(item.getUid(), item.getNickname(), Uri.parse(item.getHeadpic()));
//                    if (userInfo != null) {
//                        Friend friend = CharacterParser.getInstance().generateFriendFromUserInfo(userInfo);
//                        NavUtils.gotoAddUserDataActivity(BlackActivity.this, friend);
//                    }
                }
            }
        });
    }

    private QuickAdapter<FriendRequestListResp> mAdapter;

    @Override
    public void initData() {
        ItemHelper mItemHelper = new ItemHelper();
        mAdapter = mItemHelper.setAdapter(this, new ArrayList());
        mListView.setAdapter(mAdapter);
//        mInterfacePresenter.getBlacklist(pageNum, pageSize);
    }

    @Override
    public void setHeader() {
        super.setHeader();
        rlTitle.setVisibility(View.VISIBLE);
        ivTopBack.setVisibility(View.VISIBLE);
        tvTopTitle.setText("demoList");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    @Override
    public void onError(BaseResp s) {
        mPullToRefreshListView.onRefreshComplete();
        Utils.bequit(s,this);
    }

    @Override
    public void onSuccess(BaseResp s) {
        mPullToRefreshListView.onRefreshComplete();
        if (s.getUrl().equals(URLUtil.url_get_getVerifyCode)) {//
            if (s.getSuccess().equals("true")) {
                List<FriendRequestListResp> list = JsonAnalysisUtil.JsonFriendRequestListRespBean(s.getData());
                if (pageNum == 1) {
                    if (list.size() == 0) {
                        mAdapter.clear();
                        mPullToRefreshListView.setVisibility(View.GONE);
                        tvwu.setVisibility(View.VISIBLE);
                    } else {
                        mPullToRefreshListView.setVisibility(View.VISIBLE);
                        tvwu.setVisibility(View.GONE);
                        mAdapter.clear();
                        mAdapter.replaceAll(list);
                    }
                } else {
                    if (list.size() == 0) {
                        ToastUtil.makeText(DemoListActivity.this, "没有更多数据");
                    } else {
                        mAdapter.addAll(list);
                    }
                }
                pageNum++;
            } else {
                ToastUtil.makeText(DemoListActivity.this, s.getMessage());
            }
        } else {
            ToastUtil.makeText(DemoListActivity.this, s.getMessage());
        }
    }

    @Override
    public void showLoading() {
        super.showLoading();
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
